package com.jlastu.znb.view;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.jlastu.znb.R;

public class SecondActivity extends AppCompatActivity {

    private LinearLayout llTabOne, llTabTwo, llTabAdd, llTabMessage, llTabMine;
    private ImageView ivTabOne, ivTabTwo, ivTabAdd, ivTabMessage, ivTabMine;
    private TextView tvTabOne, tvTabTwo, tvTabAdd, tvTabMessage, tvTabMine;
    private SellerHomeFragment mSellerHomeFragment;
    private BuyerHomeFragment mBuyerHomeFragment;
    private NongyouFragment mNongyouFragment;
    private MessageFragment mMessageFragment;
    private MineFragment mMineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.second_fragment_content2, new SellerHomeFragment())
                    .commitNow();
        }
        mFragmentManager = getSupportFragmentManager();
        mSellerHomeFragment = new SellerHomeFragment();
        mBuyerHomeFragment = new BuyerHomeFragment();
        mNongyouFragment = new NongyouFragment();
        mMineFragment = new MineFragment();
        mMessageFragment = new MessageFragment();
        // 初始化底部导航栏的各个组件
        llTabOne = findViewById(R.id.ll_tab_one);
        llTabTwo = findViewById(R.id.ll_tab_two);
        llTabAdd = findViewById(R.id.ll_tab_add);
        llTabMessage = findViewById(R.id.ll_tab_message);
        llTabMine = findViewById(R.id.ll_tab_mine);

        ivTabOne = findViewById(R.id.iv_tab_one);
        ivTabTwo = findViewById(R.id.iv_tab_two);
        ivTabAdd = findViewById(R.id.iv_tab_add);
        ivTabMessage = findViewById(R.id.iv_tab_message);
        ivTabMine = findViewById(R.id.iv_tab_mine);

        tvTabOne = findViewById(R.id.tv_tab_one);
        tvTabTwo = findViewById(R.id.tv_tab_two);
        tvTabAdd = findViewById(R.id.tv_tab_add);
        tvTabMessage = findViewById(R.id.tv_tab_message);
        tvTabMine = findViewById(R.id.tv_tab_mine);

        // 设置点击事件
        setupTabSelection(0);

        llTabOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setupTabSelection(0);
                // 切换到首页Fragment
            }
        });

        llTabTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setupTabSelection(1);
                // 切换到农友圈Fragment
            }
        });

        llTabAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setupTabSelection(2);
                // 切换到发布Fragment
            }
        });

        llTabMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setupTabSelection(3);
                // 切换到消息Fragment
            }
        });

        llTabMine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setupTabSelection(4);
                // 切换到我的Fragment
            }
        });
    }

    private void setupTabSelection(int position) {
        // 重置所有标签的样式
        llTabOne.setSelected(false);
        llTabTwo.setSelected(false);
        llTabAdd.setSelected(false);
        llTabMessage.setSelected(false);
        llTabMine.setSelected(false);

        // 根据位置设置选中的标签样式
        switch (position) {
            case 0:
                llTabOne.setSelected(true);
                changeFragment(mSellerHomeFragment);
                break;
            case 1:
                llTabTwo.setSelected(true);
                // 切换到农友圈Fragment
                changeFragment(mNongyouFragment);
                break;
            case 2:
                llTabAdd.setSelected(true);
                // 切换到发布Fragment
                Intent intent = new Intent(SecondActivity.this, FaBuActivity.class);
                startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(
                        SecondActivity.this).toBundle());
                break;
            case 3:
                llTabMessage.setSelected(true);
               changeFragment(mMessageFragment);
                break;
            case 4:
                llTabMine.setSelected(true);
                // 切换到我的Fragment
                changeFragment(mMineFragment);
                break;
        }
    }
    private Fragment mContentFragment;
    private FragmentManager mFragmentManager;
    public void changeFragment(Fragment to) {
        if (mContentFragment != to) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            if (mContentFragment != null) {
                transaction.hide(mContentFragment);
            }
            if (to.isAdded()) {
                transaction.show(to);
            } else {
                transaction.add(R.id.second_fragment_content2, to);
            }
            transaction.commitAllowingStateLoss();
            mContentFragment = to;
        }
    }
}